﻿using System.Windows;
using System;
using System.Collections;
using System.Collections.Generic;
using Messenia.GUI.ServiceResolver;
using Messenia.Data.Views;
using Messenia.Data.Interfaces;


namespace Messenia.GUI
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainForm :  IMainView
    {


        public MainForm ()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            LoadProjects();
        }      

        private void LoadProjects()
        {
            
            var projects = NHAOPResolver.Resolve<IProject>();
            //LoadView(projects.GetAll());
            listView1.ItemsSource = projects.GetAll(0) as List<Messenia.Data.Models.Project>;
            
        }
        void LoadView(IEnumerable objects) {        
            IList<string> propsName = new List<String>();        
            
            IEnumerator classesEnum = objects.GetEnumerator();
            if (classesEnum.MoveNext()) {
            var   obj = classesEnum.Current;
                foreach (var item in obj.GetType().GetProperties()) {
                    propsName.Add(item.Name);
                }
                objects.GetEnumerator().Reset();

            } else
                return;

           
            foreach (var item in objects) {
                listView1.Items.Add(item);
            }
            
        }
     

      

        void ReloadList(object sender, EventArgs e)
        {
            listView1.ItemsSource = NHAOPResolver.Resolve<IProject>().GetAll(0);
        }

        void addProjectForm_Closed(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

       
        private void btnMinimize_Click(object sender , RoutedEventArgs e) {
            WindowState = WindowState.Minimized;
        }

        private void button3_Click(object sender , RoutedEventArgs e) {
            this.Close();            
        }

        private void btnAddStatus_Click (object sender, RoutedEventArgs e) {
            AddStatus frmStatus = new AddStatus();
            frmStatus.ShowDialog();
        }

        private void BtnAddProject_click (object sender, RoutedEventArgs e) {
            AddProject addProjectForm = new AddProject (this);
            addProjectForm.Closed += new EventHandler (ReloadList);
            Window owner = Window.GetWindow (this);

            Window alreadyOpenedWindow = null;
            foreach ( Window openedWindow in owner.OwnedWindows ) {
                if ( openedWindow.GetType () == typeof (AddProject) )
                    alreadyOpenedWindow = openedWindow;
            }

            if ( alreadyOpenedWindow == null ) {
                addProjectForm.Owner = owner;
                addProjectForm.Show ();
            } else {
                alreadyOpenedWindow.Focus ();
            }
            
        }
    }
    
   

}
